import router from './router'
import store from '@/store'
import md5 from 'md5'
import { MessageBox } from 'element-ui'

const whiteRouteList = ['Login', 'AgentApply', 'Welcome']
router.beforeEach(async (to, from, next) => {
  const hasToken = localStorage.getItem('LG_TK')
  if (hasToken) {
    const hasRoutes = store.getters.routes.length > 0
    let resourceSet = store.getters.resourceSet
    if (!hasRoutes) {
      try {
        resourceSet = await store.dispatch('getGlobalResourceSet')
      } catch (error) {
        console.log(error || '请求错误')
      }
    }
    if (
      localStorage.getItem('LG_MP') === md5('user_123456') &&
      ['MERCHANT', 'SELF_OPERATION_MERCHANT', 'STAFF'].includes(
        store.getters.roleType
      ) &&
      resourceSet.includes(
        'route:/web/myInformation-management/basicInformation-management/modifyPassword-management/'
      ) &&
      to.name !== 'MyselfRevisePas' &&
      to.name !== 'Login'
    ) {
      next()
      // MessageBox.alert('请修改系统默认密码,修改成功后才可操作系统！', '提示', {
      //   center: true,
      //   customClass: 'tip-dialog__wrapper',
      // })
    } else if (
      (!to.meta.resourceId || resourceSet.includes(to.meta.resourceId)) &&
      to.name !== 'Login'
    ) {
      next()
    } else {
      next('/welcome')
    }
    // ;(!to.meta.resourceId || resourceSet.includes(to.meta.resourceId)) &&
    // to.name !== 'Login'
    //   ? next()
    //   : next('/welcome')
  } else {
    if (whiteRouteList.includes(to.name)) {
      next()
    } else {
      next({ name: 'Login' })
    }
  }
})
